<?php
//**---------------------------------------------------------------------+
//** 服务层文件 -- 店铺
//**---------------------------------------------------------------------+
//** 版权所有：江西佰商科技有限公司. 官网：https://www.tigshop.com
//**---------------------------------------------------------------------+
//** 作者：Tigshop团队，yq@tigshop.com
//**---------------------------------------------------------------------+
//** 提示：Tigshop商城系统为非免费商用系统，未经授权，严禁使用、修改、发布
//**---------------------------------------------------------------------+

namespace app\service\admin\promotion;

use app\model\promotion\Promotion;
use app\service\common\BaseService;

/**
 * 活动管理服务类
 */
class PromotionService extends BaseService
{

    public function __construct(Promotion $promotion)
    {
        $this->model = $promotion;
    }


    /**
     * 筛选查询
     *
     * @param array $filter
     * @return object
     */
    protected function filterQuery(array $filter): object
    {
        $query = $this->model->query();
        // 处理筛选条件
        if (isset($filter['type']) && !empty($filter['type'])) {
            $query->whereIn('type', $filter['type']);
        }
        if (isset($filter['shop_id']) && $filter['shop_id'] > -1) {
            $query->where('shop_id', $filter['shop_id']);
        }
        if (isset($filter['time_type']) && $filter['time_type'] == 1) {
            $query->where(function ($query) {
                $query->where(function ($query) {
                    $query->where('start_time', '<=', time())->where('end_time', '>=', time());
                })->whereOr(function ($query) {
                    $query->where("start_time", 0)->where("end_time", 0);
                });
            });
        } elseif (isset($filter['time_type']) && $filter['time_type'] == 2) {
            $query->where(function ($query) {
                $query->where(function ($query) {
                    $query->where('start_time', '<=', time())->where("end_time", "<=",
                        time() + 7 * 24 * 3600)->where('end_time', '>=', time())->where('end_time', '<>', 0);
                });
            });

        } elseif (isset($filter['time_type']) && $filter['time_type'] == 3) {
            $query->where('start_time', '>', time());
        }


        if (isset($filter['is_delete'])){
            $query->where('is_delete', $filter['is_delete']);
        }

        if (isset($filter['is_available'])) {
            $query->where('is_available', $filter['is_available']);
        }
        return $query;
    }


}
